//
//  UIGestureRecognizer+YHExtension.swift
//  YHKit
//
//  Created by amovision on 2025/8/6.
//

import UIKit

private enum AssociatedKeys {
  static var gestureBlockWrapper: Int8 = 0
}

private class GestureBlockWrapper {
  let block: (UIGestureRecognizer) -> Void

  init(block: @escaping (UIGestureRecognizer) -> Void) {
    self.block = block
  }

  @objc func invoke(_ gesture: UIGestureRecognizer) {
    block(gesture)
  }
}

public extension UIGestureRecognizer {
  /// 关联的闭包包装器
  private var blockWrapper: GestureBlockWrapper? {
    get { objc_getAssociatedObject(self, &AssociatedKeys.gestureBlockWrapper) as? GestureBlockWrapper }
    set { objc_setAssociatedObject(self, &AssociatedKeys.gestureBlockWrapper, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) }
  }

  /// 使用闭包初始化手势
  convenience init(block: @escaping (UIGestureRecognizer) -> Void) {
    self.init()
    let wrapper = GestureBlockWrapper(block: block)
    self.blockWrapper = wrapper
    addTarget(wrapper, action: #selector(GestureBlockWrapper.invoke(_:)))
  }
}

// MARK: - 具体手势的扩展

public extension UITapGestureRecognizer {
  /// 点击手势初始化（带参数和闭包）
  convenience init(taps: Int = 1, touches: Int = 1, block: @escaping (UITapGestureRecognizer) -> Void) {
    self.init { gesture in
      guard let tapGesture = gesture as? UITapGestureRecognizer else { return }
      block(tapGesture)
    }
    numberOfTapsRequired = taps
    numberOfTouchesRequired = touches
  }
}

public extension UILongPressGestureRecognizer {
  /// 长按手势初始化
  convenience init(
    taps: Int = 0,
    touches: Int = 1,
    minDuration: TimeInterval = 0.5,
    allowMovement: CGFloat = 10,
    block: @escaping (UILongPressGestureRecognizer) -> Void
  ) {
    self.init { gesture in
      guard let longPress = gesture as? UILongPressGestureRecognizer else { return }
      block(longPress)
    }
    self.numberOfTapsRequired = taps
    self.numberOfTouchesRequired = touches
    self.minimumPressDuration = minDuration
    self.allowableMovement = allowMovement
  }
}

public extension UIPanGestureRecognizer {
  /// 拖动手势初始化
  convenience init(minTouches: Int, maxTouches: Int, block: @escaping (UIPanGestureRecognizer) -> Void) {
    self.init { gesture in
      guard let pan = gesture as? UIPanGestureRecognizer else { return }
      block(pan)
    }
    self.minimumNumberOfTouches = minTouches
    self.maximumNumberOfTouches = maxTouches
  }
}

public extension UISwipeGestureRecognizer {
  /// 滑动手势初始化
  convenience init(touches: Int, direction: UISwipeGestureRecognizer.Direction, block: @escaping (UISwipeGestureRecognizer) -> Void) {
    self.init { gesture in
      guard let swipe = gesture as? UISwipeGestureRecognizer else { return }
      block(swipe)
    }
    self.numberOfTouchesRequired = touches
    self.direction = direction
  }
}

public extension UIRotationGestureRecognizer {
  /// 旋转手势初始化
  convenience init(rotation: CGFloat, block: @escaping (UIRotationGestureRecognizer) -> Void) {
    self.init { gesture in
      guard let rotation = gesture as? UIRotationGestureRecognizer else { return }
      block(rotation)
    }
    self.rotation = rotation
  }
}

public extension UIPinchGestureRecognizer {
  /// 捏合手势初始化
  convenience init(scale: CGFloat, block: @escaping (UIPinchGestureRecognizer) -> Void) {
    self.init { gesture in
      guard let pinch = gesture as? UIPinchGestureRecognizer else { return }
      block(pinch)
    }
    self.scale = scale
  }
}
